<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Hash;
use App\Models\Usersrecharges;
use Pay;

class PayController extends Controller
{
    public function wechatQrcode(Request $request)
    {
        $users_id = $request->users_id;
        if (empty($request->total_fee)) {
            $qrcode_url = "";
            $out_trade_no = "";
            $body = "";
            $total_fee = "";
        } else {
            $config = [
                // 公众号 APPID
                'app_id' => 'wx53ab6b46d8644ad2',
                // 微信支付分配的微信商户号
                'mch_id' => '1607321508',
                // 微信支付异步通知地址
                'notify_url' => 'https://bab.moreqifu.cn/api/version/1.0/miniapp/wechatBack',
                // 微信支付签名秘钥
                'key' => '2n7985e4d24fd9d4c37e18ecod4d7281',
                // 客户端证书路径，退款、红包等需要用到。请填写绝对路径，linux 请确保权限问题。pem 格式。
                'cert_client' => public_path() . '/wechat/apiclient_cert.pem',
                // 客户端秘钥路径，退款、红包等需要用到。请填写绝对路径，linux 请确保权限问题。pem 格式。
                'cert_key' => public_path() . '/wechat/apiclient_key.pem',
                // optional，默认 warning；日志路径为：sys_get_temp_dir().'/logs/yansongda.pay.log'
                'log' => [
                    'file' => storage_path('logs/pay.log'),
                    'level' => 'error'
                ],
            ];
            $out_trade_no = md5(time().rand(1000,9999));
            $order = [
                'out_trade_no' => $out_trade_no,
                'body' => $request->body,
                'total_fee'      => $request->total_fee*100,
            ];
            $result = Pay::wechat($config)->scan($order);
            $qrcode_url = $result->code_url;
            $body = $request->body;
            $total_fee = $request->total_fee;
            // 创建充值记录
            Usersrecharges::usersRechargesInsert($users_id, $out_trade_no, $request->total_fee, 2);
        }
        return view('admin.pay.wechat_scan_pay',["qrcode_url"=>$qrcode_url,"out_trade_no"=>$out_trade_no,"users_id"=>$users_id,"body"=>$body,"total_fee"=>$total_fee]);
    }

    public function wechatQrcodeStatus(Request $request)
    {
        $usersrecharges = Usersrecharges::usersRechargesById($request->out_trade_no);
        $config = [
            // 公众号 APPID
            'appid' => 'wxfc543e0476ce33a6',
            // 微信支付分配的微信商户号
            'mch_id' => '1607321508',
            // 微信支付异步通知地址
            'notify_url' => 'https://bab.moreqifu.cn/api/version/1.0/miniapp/wechatBack',
            // 微信支付签名秘钥
            'key' => '2n7985e4d24fd9d4c37e18ecod4d7281',
            // 客户端证书路径，退款、红包等需要用到。请填写绝对路径，linux 请确保权限问题。pem 格式。
            'cert_client' => public_path() . '/wechat/apiclient_cert.pem',
            // 客户端秘钥路径，退款、红包等需要用到。请填写绝对路径，linux 请确保权限问题。pem 格式。
            'cert_key' => public_path() . '/wechat/apiclient_key.pem',
            // optional，默认 warning；日志路径为：sys_get_temp_dir().'/logs/yansongda.pay.log'
            'log' => [
                'file' => storage_path('logs/pay.log'),
                'level' => 'error'
            ],
        ];
        $order = [
            'out_trade_no' => $request->out_trade_no,
        ];
        $result = Pay::wechat($config)->find($order);
        if ($result->trade_state == "SUCCESS") {
            return redirect("/admin/users/users_detial/".$usersrecharges->users_id)->with("status","success")->with("msg","订单已支付成功！");
        } else {
            return redirect("/admin/users/users_detial/".$usersrecharges->users_id)->with("status","error")->with("msg","订单未支付！");
        }
    }
}